package com.powernode.lambda.stream;

import com.powernode.lambda.Student;

import java.util.stream.Stream;

/**
 * 测试stream相关Api reduce化简、mapToInt流转整数
 * 适合：求最大值、最小值、求和
 * reduce((x,y)->r)
 * reduce(init,(x,y)->r)
 * reduce(init,(x,y)->r,(r1.r2)->r)
 */
public class Stream08 {
    public static void main(String[] args) {
        Stream<Student> stream = Stream.of(
                new Student("张无忌", 26, "男"),
                new Student("赵敏", 22, "女"),
                new Student("周芷若", 21, "女"),
                new Student("女波斯圣使", 30, "女"),
                new Student("灭绝师太", 30, "女"),
                new Student("张三丰", 100, "男")
        );

        //求年龄最大的
        //System.out.println(stream.reduce((x, y) -> x.getAge() >= y.getAge() ? x : y));

        //求年龄最大的,添加第一个参数为默认值，如果流中无数据即返回默认值
        //System.out.println(stream.reduce(new Student("null",0,"null"),(x, y) -> x.getAge() >= y.getAge() ? x : y));

        //求流中数据量
        //System.out.println(stream.map(s -> 1).reduce(0, (x, y) -> x + y));
        //System.out.println(stream.count());

        //求最大值max
        //System.out.println(stream.max((x, y) -> x.getAge() > y.getAge() ? 1 : x.getAge() == y.getAge() ? 0 : -1));

        //求总年龄数mapToInt
        System.out.println(stream.mapToInt(stu -> stu.getAge()).sum());
    }

}
